package richbar.com.richbar.view.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import richbar.com.richbar.R;
import richbar.com.richbar.activity.me.ChangePayPwdAc;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.BaseBean;
import richbar.com.richbar.bean.CodeBean;
import richbar.com.richbar.help.CountDownTimerHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;

/**
 * Created by QiYang on 2018/12/12.
 */

public class InputDialog {
    public static void show(Activity mActivity, String title, final Callback callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        View view = View.inflate(mActivity, R.layout.dialog_input, null);
        builder.setView(view);
        builder.setCancelable(false);
        TextView titleTv = view.findViewById(R.id.title);
        titleTv.setText(title);
        TextView besureTv = view.findViewById(R.id.besure);
        TextView cancleTv = view.findViewById(R.id.cancle);
        final TextView input = view.findViewById(R.id.input);
        final AlertDialog dialog = builder.create();
        cancleTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        besureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtil.isEmpty(StringUtil.getStr(input))) {
                    return;
                }
                callback.onBesure(StringUtil.getStr(input));
                dialog.cancel();
            }
        });
        dialog.show();
    }


    public static void shows(final Activity mActivity, String title, final Callbacks callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        View view = View.inflate(mActivity, R.layout.dialog_inputs, null);
        builder.setView(view);
        builder.setCancelable(false);
        TextView titleTv = view.findViewById(R.id.title);
        titleTv.setText(title);
        final EditText original_pay_pwd = view.findViewById(R.id.original_pay_pwd);
        final EditText tel = view.findViewById(R.id.tel);
        final EditText code = view.findViewById(R.id.code);
        final TextView get_code = view.findViewById(R.id.get_code);
        final TextView cancleTv = view.findViewById(R.id.cancle);
        final TextView besureTv = view.findViewById(R.id.besure);
        final TextView input = view.findViewById(R.id.input);
        final AlertDialog dialogs = builder.create();
        cancleTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogs.cancel();
                callback.onCancel();
            }
        });
        besureTv.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            if (original_pay_pwd.getText().toString().length() != 6) {
                                                ToastUtil.showToast(mActivity, "请输入六位数字密码");
                                            } else {

                                                if (code.getText().toString().length() > 0) {
                                                    if (original_pay_pwd.getText().toString().length() == 6) {

                                                        NetWorkUtil.setPayPwd(tel.getText().toString(), code.getText().toString(), original_pay_pwd.getText().toString(), new HttpUtils.HttpCallback() {
                                                            @Override
                                                            public void onSuccess(String data) {
                                                                final BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                                                                if (baseBean.getCode().equals("200"))
                                                                    BaseApplication.isSetPayPwd = "1";
                                                                dialogs.cancel();
                                                                ToastUtil.showToast(mActivity, baseBean.getMsg());
                                                            }

                                                            @Override
                                                            public void onError(String msg) {
                                                                super.onError(msg);

                                                            }
                                                        });
                                                    } else

                                                    {
                                                        ToastUtil.showToast(mActivity, "请输入六位数字密码");
                                                    }


                                                } else {
                                                    ToastUtil.showToast(mActivity, "请输入验证码");
                                                }


                                            }


                                        }
                                    }

        );
        get_code.setOnClickListener(new View.OnClickListener()

                                    {
                                        @Override
                                        public void onClick(View v) {
                                            if (tel.getText().toString().trim().length() == 11) {
                                                new CountDownTimerHelp(mActivity, get_code, 60000, 1000).start();
                                                NetWorkUtil.getRegsinCode(Util.md5(), tel.getText().toString(), "6", new HttpUtils.HttpCallback() {
                                                    @Override
                                                    public void onSuccess(String data) {
                                                        final CodeBean codeBean = MyGson.getInstance().fromJson(data, CodeBean.class);
                                                        ToastUtil.showToast(mActivity, codeBean.getMsg());
                                                    }

                                                    @Override
                                                    public void onError(String msg) {
                                                        super.onError(msg);
                                                    }
                                                });
                                            } else

                                            {
                                                ToastUtil.showToast(mActivity, "请输入正确的手机号");
                                            }

                                        }
                                    }

        );
        dialogs.show();
    }


    public interface Callback {
        void onBesure(String s);
    }

    public interface Callbacks {
        void onBesure(String s);

        void onCancel();
    }
}
